﻿/************************************************************************     
 * 类名  :  UiParser
 * 描述  :  
 * 版本  :  v1.0         
 * 用户  :  wangxinlu  
 * 时间  :  2/22/2016 9:02:04 AM    
************************************************************************  
 * Copyright @ wh 2016 . All rights reserved.  
************************************************************************/    

///<reference path="layout/HeaderFooterLayout.ts" />
///<reference path="layout/HeaderFooterLayoutData.ts" />
///<reference path="layout/FillLayout.ts" />
///<reference path="layout/FillLayoutData.ts" />
///<reference path="layout/SashLayout.ts" />
///<reference path="layout/SashLayoutData.ts" />
///<reference path="layout/RowLayout.ts" />
///<reference path="layout/RowLayoutData.ts" />
///<reference path="Widget.ts" />
///<reference path="WidgetExtension.ts" />
namespace wh.ui {

    export class UiParser {

        private static parseHuiAttr(jQueryNode: JQuery, attrType: string): any{

            var attributeString = jQueryNode.attr(attrType);

            if (attributeString == null || attributeString == undefined || attributeString == "")
                return null;
            return JSON.parse(attributeString);
        }

        private static parseLayout(root: HTMLElement) {
            $(root).find("div[ui-layout-type='HeaderFooterLayout']").each(function () {

                var attributes: any = UiParser.parseHuiAttr($(this), "ui-layout-attr");
                if (attributes != null || attributes != undefined) {

                    var heights = attributes.heights;

                    new HeaderFooterLayout($(this)[0]
                        , new HeaderFooterLayoutData(heights[0], heights[1], heights[2]))
                        .doLayout();
                } else {
                    new HeaderFooterLayout($(this)[0]
                        ,new HeaderFooterLayoutData()).doLayout();
                }
            });

            $(root).find("div[ui-layout-type='SashLayout']").each(function () {
                var attributes = UiParser.parseHuiAttr($(this), "ui-layout-attr");

                if (attributes != null || attributes != undefined) {
                    new SashLayout($(this)[0]
                        , new SashLayoutData(attributes[0], attributes[1])).doLayout();
                } else {
                    new SashLayout($(this)[0], new SashLayoutData()).doLayout();
                }
            });

            $(root).find("div[ui-layout-type='FillLayout']").each(function () {
                new FillLayout($(this)[0], new FillLayoutData()).doLayout();
            });

            $(root).find("div[ui-layout-type='RowLayout']").each(function () {
                new RowLayout($(this)[0],new RowLayoutData()).doLayout();
            });
        }

        public static parse(root: HTMLElement) {
            this.parseLayout(root);
        }

    }
}